package com.tsing.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.Result;
import com.tsing.pojo.vo.CommentDetailsVO;
import com.tsing.service.PostDetailsService;
import com.tsing.service.impl.PostDetailsServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/12 10:14
 */
public class PostDetailsController {

    private final PostDetailsService service = new PostDetailsServiceImpl();
    public Result<List<CommentDetailsVO>> comments(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer postId = jsonObject.getInteger("postId");
        postId = (postId != null && postId > 0) ? postId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        List<CommentDetailsVO> result = service.list(postId,userId);

        return Result.success(result);
    }

    //帖子评论点赞
    public Result add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer detailsId = jsonObject.getInteger("detailsId");
        detailsId = (detailsId != null && detailsId > 0) ? detailsId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        Boolean result = service.add(detailsId,userId);

        return Result.success(result);
    }

    //取消帖子评论点赞
    public Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer detailsId = jsonObject.getInteger("detailsId");
        detailsId = (detailsId != null && detailsId > 0) ? detailsId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        Boolean result = service.delete(detailsId,userId);

        return Result.success(result);
    }

    //关注作者
    public Result addFollow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer followId = jsonObject.getInteger("followId");
        followId = (followId != null && followId > 0) ? followId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        Boolean result = service.addFollow(followId,userId);

        return Result.success(result);
    }
    //取消关注作者
    public Result deleteFollow(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer followId = jsonObject.getInteger("followId");
        followId = (followId != null && followId > 0) ? followId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        Boolean result = service.deleteFollow(followId,userId);

        return Result.success(result);
    }
}
